package codebot.processor.logic;

import codebot.processor.IScreen;

/**
 * Simulates and wrappes everything related to compiler state
 * 
 * @author Teo
 * 
 */
public final class State {

  private int mState;
  private int mStart;
  private int mEnd; // mStart & mEnd used to delimit various constructions
  private IScreen mScreen; // used to perform various (visible) actions
  private int mCodeStart; // shows where the code starts in the text

  /**
   * Constructs the initial state
   * 
   * @param screen
   * @param codeStart
   */
  public State(IScreen screen, int codeStart) {
    mState = 0;
    mStart = -1;
    mEnd = -1;
    mScreen = screen;
    mCodeStart = codeStart;
  }

  /**
   * Returns the end index of a followed construction
   * 
   * @return
   */
  public final int getEnd() {
    return mEnd;
  }

  /**
   * Returns the start index of a followed construction
   * 
   * @return
   */
  public final int getStart() {
    return mStart;
  }

  /**
   * Returns the state as an int
   * 
   * @return
   */
  public final int getState() {
    return mState;
  }

  /**
   * Setting various state variables and even performing actions. The parameters
   * that are null won't change.
   * 
   * @param state
   * @param start
   * @param end
   * @param bold
   * @param color
   * @param font
   * @param italic
   */
  public final void set(Integer state, Integer start, Integer end,
      Boolean bold, String color, String font, Boolean italic) {
    // bold color font italic
    if (state != null) {
      mState = state;
    }
    if (start != null) {
      mStart = start;
    }
    if (end != null) {
      mEnd = end;
    }
    if (bold != null) {
      mScreen.setBold(bold, mCodeStart + mStart, mCodeStart + mEnd);
    }
    if (color != null) {
      mScreen.setColor(color, mCodeStart + mStart, mCodeStart + mEnd);
    }
    if (font != null) {
      mScreen.setFont(font, mCodeStart + mStart, mCodeStart + mEnd);
    }
    if (italic != null) {
      mScreen.setItalic(italic, mCodeStart + mStart, mCodeStart + mEnd);
    }
  }

}
